package ge.telasi.tasks.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * @author dimitri
 */
@Entity
@Table(name = "flow_permissions")
@NamedQueries({
    @NamedQuery(name = "FlowPermission.getByType", query = "SELECT p FROM FlowPermission p " +
            "WHERE p.taskType = :taskType " +
            "ORDER BY p.operation, p.taskStatus, p.role, p.finalStatus")
})
public class FlowPermission implements Serializable {
    private static final long serialVersionUID = -3314924616846208308L;

    public static final int OPERATION_CHANGE_RECEIVERS = 1;
    public static final int OPERATION_CHANGE_SUBJECT = 2;
    public static final int OPERATION_CHANGE_PRIORITY = 3;
    public static final int OPERATION_CHANGE_STATUS = 4;
    public static final int OPERATION_ADD_ATTACHMENT = 5;
    public static final int OPERATION_REMOVE_ATTACHMENT = 6;
    public static final int OPERATION_DOWNLOAD_ATTACHMENT = 7;
    public static final int OPERATION_DELETE_TASK = 8;
    public static final int OPERATION_ADD_PARENT = 9;
    public static final int OPERATION_REMOVE_PARENT = 10;

    /**
     * Mapping of operations to roles.
     */
    public static Map<Integer, List<String>> OPERATION_ROLES = new HashMap();
    static {
        List<String> commonList = new ArrayList<String>();
        commonList.add(Task.ROLE_OWNER);
        commonList.add(Task.ROLE_PREPARED_BY);
        commonList.add(Task.ROLE_ASSIGNEE);
        commonList.add(Task.ROLE_CARBON_COPY);
        List<String> extendedList = new ArrayList<String>();
        extendedList.addAll(commonList);
        extendedList.add(Task.ROLE_FILE_OWNER);
        OPERATION_ROLES.put(OPERATION_CHANGE_RECEIVERS, commonList);
        OPERATION_ROLES.put(OPERATION_CHANGE_SUBJECT, commonList);
        OPERATION_ROLES.put(OPERATION_CHANGE_PRIORITY, commonList);
        OPERATION_ROLES.put(OPERATION_CHANGE_STATUS, commonList);
        OPERATION_ROLES.put(OPERATION_DELETE_TASK, commonList);
        OPERATION_ROLES.put(OPERATION_ADD_ATTACHMENT, commonList);
        OPERATION_ROLES.put(OPERATION_ADD_PARENT, commonList);
        OPERATION_ROLES.put(OPERATION_REMOVE_PARENT, commonList);
        OPERATION_ROLES.put(OPERATION_REMOVE_ATTACHMENT, extendedList);
        OPERATION_ROLES.put(OPERATION_DOWNLOAD_ATTACHMENT, extendedList);
    }

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FLOW_PERMISSIONS_SEQ")
	@SequenceGenerator(name = "FLOW_PERMISSIONS_SEQ", sequenceName = "FLOW_PERMISSIONS_SEQ", allocationSize = 1)
    private Integer id;
    @Column(name = "task_type")
    private int taskType;
    @Column(name = "role_name")
    private String role;
    @Column(name = "operation_id")
    private int operation;
    @Column(name = "task_status")
    private int taskStatus;
    @Column(name = "final_status")
    private Integer finalStatus;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public int getTaskType() {
        return taskType;
    }

    public void setTaskType(int taskType) {
        this.taskType = taskType;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public int getOperation() {
        return operation;
    }

    public void setOperation(int operation) {
        this.operation = operation;
    }

    public int getTaskStatus() {
        return taskStatus;
    }

    public void setTaskStatus(int taskStatus) {
        this.taskStatus = taskStatus;
    }

    public Integer getFinalStatus() {
        return finalStatus;
    }

    public void setFinalStatus(Integer finalStatus) {
        this.finalStatus = finalStatus;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof FlowPermission)) {
            return false;
        }
        if (id == null) {
            return false;
        }
        FlowPermission myobj = (FlowPermission) obj;
        return id.equals(myobj.getId());
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

}
